#include <QtGui>

#include "src/color.h"
#include "springsDialog.h"
#include "fabricTreeItem.h"
#include "springTreeItem.h"
#include "treeItem.h"

SpringsDialog::SpringsDialog(TreeItem *_item, QWidget *_parent)
    : QDialog(_parent), item(_item){

    setWindowTitle(QString::fromUtf8("Modificando todas as molas"));

    colorRStructural = 255;
    colorGStructural = 255;
    colorBStructural = 255;
    colorAlphaStructural = 255;
    colorRShear = 255;
    colorGShear = 255;
    colorBShear = 255;
    colorAlphaShear = 255;
    colorRFlexion = 255;
    colorGFlexion = 255;
    colorBFlexion = 255;
    colorAlphaFlexion = 255;

    ksStructural = 2200;
    kdStructural = 2;
    ksShear = 50;
    kdShear = 1;
    ksFlexion = 2;
    kdFlexion = 0.0005;

    createLayout();
}

SpringsDialog::~SpringsDialog(){
}

void SpringsDialog::createLayout(){
    //--------------------------------------Constantes - Estrutural
    QDoubleSpinBox *ksStructuralDoubleSpinBox = new QDoubleSpinBox();
    ksStructuralDoubleSpinBox->setRange(0.000001, 50000.0);
    ksStructuralDoubleSpinBox->setSingleStep(1.0);
    ksStructuralDoubleSpinBox->setDecimals(6);
    ksStructuralDoubleSpinBox->setValue(ksStructural);
    connect(ksStructuralDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKsStructural(double)));

    QDoubleSpinBox *kdStructuralDoubleSpinBox = new QDoubleSpinBox();
    kdStructuralDoubleSpinBox->setRange(0.0, 50000.0);
    kdStructuralDoubleSpinBox->setSingleStep(1.0);
    kdStructuralDoubleSpinBox->setDecimals(6);
    kdStructuralDoubleSpinBox->setValue(kdStructural);
    connect(kdStructuralDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKdStructural(double)));

    QHBoxLayout *ksStructuralLayout = new QHBoxLayout;
    ksStructuralLayout->addWidget(new QLabel ( QString::fromUtf8("Ks: ") ));
    ksStructuralLayout->addWidget(ksStructuralDoubleSpinBox);

    QHBoxLayout *kdStructuralLayout = new QHBoxLayout;
    kdStructuralLayout->addWidget(new QLabel ( QString::fromUtf8("Kd: ") ));
    kdStructuralLayout->addWidget(kdStructuralDoubleSpinBox);

    QVBoxLayout *kStructuralLayout = new QVBoxLayout;
    kStructuralLayout->addLayout(ksStructuralLayout);
    kStructuralLayout->addLayout(kdStructuralLayout);
    kStructuralLayout->addStretch(1);

    kStructuralGroupBox = new QGroupBox(QString::fromUtf8("Constantes"));
    kStructuralGroupBox->setCheckable(true);
    kStructuralGroupBox->setChecked(false);
    kStructuralGroupBox->setLayout(kStructuralLayout);


    //--------------------------------------Cor - estrutural
    colorStructuralPushButton = new QPushButton(tr("Cor"));
    colorStructuralPushButton->setEnabled(false);
    connect(colorStructuralPushButton, SIGNAL(clicked()), this, SLOT(changeColorStructural()));
    setButtonColor(colorStructuralPushButton, colorRStructural, colorGStructural, colorBStructural, colorAlphaStructural);

    QHBoxLayout *colorStructuralLayout = new QHBoxLayout;
    colorStructuralLayout->addWidget(colorStructuralPushButton);

    colorStructuralGroupBox = new QGroupBox(QString::fromUtf8("Cor das molas"));
    colorStructuralGroupBox->setCheckable(true);
    colorStructuralGroupBox->setChecked(false);
    colorStructuralGroupBox->setLayout(colorStructuralLayout);
    connect(colorStructuralGroupBox, SIGNAL(toggled(bool)), colorStructuralPushButton, SLOT(setEnabled(bool)));


    QVBoxLayout *structuralLayout = new QVBoxLayout;
    structuralLayout->addWidget(kStructuralGroupBox);
    structuralLayout->addWidget(colorStructuralGroupBox);

    QGroupBox *structuralGroupBox = new QGroupBox(QString::fromUtf8("Molas estruturais"));
    structuralGroupBox->setLayout(structuralLayout);









    //--------------------------------------Constantes - Cisalhamento
    QDoubleSpinBox *ksShearDoubleSpinBox = new QDoubleSpinBox();
    ksShearDoubleSpinBox->setRange(0.000001, 50000.0);
    ksShearDoubleSpinBox->setSingleStep(1.0);
    ksShearDoubleSpinBox->setDecimals(6);
    ksShearDoubleSpinBox->setValue(ksShear);
    connect(ksShearDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKsShear(double)));

    QDoubleSpinBox *kdShearDoubleSpinBox = new QDoubleSpinBox();
    kdShearDoubleSpinBox->setRange(0.0, 50000.0);
    kdShearDoubleSpinBox->setSingleStep(1.0);
    kdShearDoubleSpinBox->setDecimals(6);
    kdShearDoubleSpinBox->setValue(kdShear);
    connect(kdShearDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKdShear(double)));

    QHBoxLayout *ksShearLayout = new QHBoxLayout;
    ksShearLayout->addWidget(new QLabel ( QString::fromUtf8("Ks: ") ));
    ksShearLayout->addWidget(ksShearDoubleSpinBox);

    QHBoxLayout *kdShearLayout = new QHBoxLayout;
    kdShearLayout->addWidget(new QLabel ( QString::fromUtf8("Kd: ") ));
    kdShearLayout->addWidget(kdShearDoubleSpinBox);

    QVBoxLayout *kShearLayout = new QVBoxLayout;
    kShearLayout->addLayout(ksShearLayout);
    kShearLayout->addLayout(kdShearLayout);
    kShearLayout->addStretch(1);

    kShearGroupBox = new QGroupBox(QString::fromUtf8("Constantes"));
    kShearGroupBox->setCheckable(true);
    kShearGroupBox->setChecked(false);
    kShearGroupBox->setLayout(kShearLayout);


    //--------------------------------------Cor - cisalhamento
    colorShearPushButton = new QPushButton(tr("Cor"));
    colorShearPushButton->setEnabled(false);
    connect(colorShearPushButton, SIGNAL(clicked()), this, SLOT(changeColorShear()));
    setButtonColor(colorShearPushButton, colorRShear, colorGShear, colorBShear, colorAlphaShear);

    QHBoxLayout *colorShearLayout = new QHBoxLayout;
    colorShearLayout->addWidget(colorShearPushButton);

    colorShearGroupBox = new QGroupBox(QString::fromUtf8("Cor das molas"));
    colorShearGroupBox->setCheckable(true);
    colorShearGroupBox->setChecked(false);
    colorShearGroupBox->setLayout(colorShearLayout);
    connect(colorShearGroupBox, SIGNAL(toggled(bool)), colorShearPushButton, SLOT(setEnabled(bool)));


    QVBoxLayout *shearLayout = new QVBoxLayout;
    shearLayout->addWidget(kShearGroupBox);
    shearLayout->addWidget(colorShearGroupBox);

    QGroupBox *shearGroupBox = new QGroupBox(QString::fromUtf8("Molas de cisalhamento"));
    shearGroupBox->setLayout(shearLayout);










    //--------------------------------------Constantes - Estrutural
    QDoubleSpinBox *ksFlexionDoubleSpinBox = new QDoubleSpinBox();
    ksFlexionDoubleSpinBox->setRange(0.000001, 50000.0);
    ksFlexionDoubleSpinBox->setSingleStep(1.0);
    ksFlexionDoubleSpinBox->setDecimals(6);
    ksFlexionDoubleSpinBox->setValue(ksFlexion);
    connect(ksFlexionDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKsFlexion(double)));

    QDoubleSpinBox *kdFlexionDoubleSpinBox = new QDoubleSpinBox();
    kdFlexionDoubleSpinBox->setRange(0.0, 50000.0);
    kdFlexionDoubleSpinBox->setSingleStep(1.0);
    kdFlexionDoubleSpinBox->setDecimals(6);
    kdFlexionDoubleSpinBox->setValue(kdFlexion);
    connect(kdFlexionDoubleSpinBox, SIGNAL(valueChanged(double)), this, SLOT(changeKdFlexion(double)));

    QHBoxLayout *ksFlexionLayout = new QHBoxLayout;
    ksFlexionLayout->addWidget(new QLabel ( QString::fromUtf8("Ks: ") ));
    ksFlexionLayout->addWidget(ksFlexionDoubleSpinBox);

    QHBoxLayout *kdFlexionLayout = new QHBoxLayout;
    kdFlexionLayout->addWidget(new QLabel ( QString::fromUtf8("Kd: ") ));
    kdFlexionLayout->addWidget(kdFlexionDoubleSpinBox);

    QVBoxLayout *kFlexionLayout = new QVBoxLayout;
    kFlexionLayout->addLayout(ksFlexionLayout);
    kFlexionLayout->addLayout(kdFlexionLayout);
    kFlexionLayout->addStretch(1);

    kFlexionGroupBox = new QGroupBox(QString::fromUtf8("Constantes"));
    kFlexionGroupBox->setCheckable(true);
    kFlexionGroupBox->setChecked(false);
    kFlexionGroupBox->setLayout(kFlexionLayout);


    //--------------------------------------Cor - estrutural
    colorFlexionPushButton = new QPushButton(tr("Cor"));
    colorFlexionPushButton->setEnabled(false);
    connect(colorFlexionPushButton, SIGNAL(clicked()), this, SLOT(changeColorFlexion()));
    setButtonColor(colorFlexionPushButton, colorRFlexion, colorGFlexion, colorBFlexion, colorAlphaFlexion);

    QHBoxLayout *colorFlexionLayout = new QHBoxLayout;
    colorFlexionLayout->addWidget(colorFlexionPushButton);

    colorFlexionGroupBox = new QGroupBox(QString::fromUtf8("Cor das molas"));
    colorFlexionGroupBox->setCheckable(true);
    colorFlexionGroupBox->setChecked(false);
    colorFlexionGroupBox->setLayout(colorFlexionLayout);
    connect(colorFlexionGroupBox, SIGNAL(toggled(bool)), colorFlexionPushButton, SLOT(setEnabled(bool)));


    QVBoxLayout *flexionLayout = new QVBoxLayout;
    flexionLayout->addWidget(kFlexionGroupBox);
    flexionLayout->addWidget(colorFlexionGroupBox);

    QGroupBox *flexionGroupBox = new QGroupBox(QString::fromUtf8("Molas de flexão"));
    flexionGroupBox->setLayout(flexionLayout);











    //--------------------------------------Botões
    QDialogButtonBox *buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
    connect(buttonBox, SIGNAL(accepted()), this, SLOT(acceptChange()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    buttonBox->setLocale(QLocale(QLocale::Portuguese, QLocale::Brazil));

    QHBoxLayout *springsLayout = new QHBoxLayout;
    springsLayout->addWidget(structuralGroupBox);
    springsLayout->addWidget(shearGroupBox);
    springsLayout->addWidget(flexionGroupBox);

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addLayout(springsLayout);
    mainLayout->addWidget(buttonBox);
    mainLayout->addStretch(1);

    setLayout(mainLayout);
}

void SpringsDialog::setButtonColor(QPushButton *_colorPushButton, double _colorR, double _colorG, double _colorB, double _colorAlpha){
    //    QString colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n" "background-color: rgb(85, 0, 0, 255);");
    QString colorStr;
    if(_colorR + _colorB + _colorG + _colorAlpha < 600){
        colorStr = QString::fromUtf8("color: rgb(255, 255, 255);\n");
    }else{
        colorStr = QString::fromUtf8("color: rgb(0, 0, 0);\n");
    }
    colorStr.append(QString::fromUtf8("background-color: rgb("));
    colorStr.append(QString::number(_colorR));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(_colorG));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(_colorB));
    colorStr.append(QString::fromUtf8(", "));
    colorStr.append(QString::number(_colorAlpha));
    colorStr.append(QString::fromUtf8(");"));

    _colorPushButton->setStyleSheet(colorStr);
}

void SpringsDialog::acceptChange(){
    bool changeColorStructural = colorStructuralGroupBox->isChecked();
    bool changeColorShear = colorShearGroupBox->isChecked();
    bool changeColorFlexion = colorFlexionGroupBox->isChecked();
    bool changeKStructural = kStructuralGroupBox->isChecked();
    bool changeKShear = kShearGroupBox->isChecked();
    bool changeKFlexion = kFlexionGroupBox->isChecked();

    int size = item->childCount();
	for(int i = 0; i < size; i++)
	{
	    Spring *s = ((SpringTreeItem *)item->child(i))->getSpring();

        switch(s->getType())
        {
            case FLEXION:
            {
                if(changeColorFlexion) s->setColor(Color(colorRFlexion / 255.0, colorGFlexion / 255.0, colorBFlexion / 255.0, colorAlphaFlexion / 255.0));
                if(changeKFlexion) s->setKsAndKd(ksFlexion, kdFlexion);
                break;
            }
            case SHEAR:
            {
                if(changeColorShear) s->setColor(Color(colorRShear / 255.0, colorGShear / 255.0, colorBShear / 255.0, colorAlphaShear / 255.0));
                if(changeKShear) s->setKsAndKd(ksShear, kdShear);
                break;
            }
            default:
            {
                if(changeColorStructural) s->setColor(Color(colorRStructural / 255.0, colorGStructural / 255.0, colorBStructural / 255.0, colorAlphaStructural / 255.0));
                if(changeKStructural) s->setKsAndKd(ksStructural, kdStructural);
                break;
            }
        }
	}

    emit done();

    accept();
}

void SpringsDialog::changeColorStructural(){
    QColor color = QColorDialog::getColor(QColor(colorRStructural, colorGStructural, colorBStructural, colorAlphaStructural), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorRStructural = color.red();
        colorGStructural = color.green();
        colorBStructural = color.blue();
        colorAlphaStructural = color.alpha();
    }
    setButtonColor(colorStructuralPushButton, colorRStructural, colorGStructural, colorBStructural, colorAlphaStructural);
}

void SpringsDialog::changeColorShear(){
    QColor color = QColorDialog::getColor(QColor(colorRShear, colorGShear, colorBShear, colorAlphaShear), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorRShear = color.red();
        colorGShear = color.green();
        colorBShear = color.blue();
        colorAlphaShear = color.alpha();
    }
    setButtonColor(colorShearPushButton, colorRShear, colorGShear, colorBShear, colorAlphaShear);
}

void SpringsDialog::changeColorFlexion(){
    QColor color = QColorDialog::getColor(QColor(colorRFlexion, colorGFlexion, colorBFlexion, colorAlphaFlexion), this, "Escolha a cor", QColorDialog::ShowAlphaChannel);
    if (color.isValid()){
        colorRFlexion = color.red();
        colorGFlexion = color.green();
        colorBFlexion = color.blue();
        colorAlphaFlexion = color.alpha();
    }
    setButtonColor(colorFlexionPushButton, colorRFlexion, colorGFlexion, colorBFlexion, colorAlphaFlexion);
}

void SpringsDialog::changeKsStructural(double _ks){
    ksStructural = _ks;
}

void SpringsDialog::changeKdStructural(double _kd){
    kdStructural = _kd;
}

void SpringsDialog::changeKsShear(double _ks){
    ksShear = _ks;
}

void SpringsDialog::changeKdShear(double _kd){
    kdShear = _kd;
}

void SpringsDialog::changeKsFlexion(double _ks){
    ksFlexion = _ks;
}

void SpringsDialog::changeKdFlexion(double _kd){
    kdFlexion = _kd;
}
